/*
 * =====================================================================================
 *
 *       Filename:  testunit.c
 *
 *    Description:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>

typedef struct TEST_ENTRY {
	char *fname;
	void (*test_func) (char *fmt, ...);
} _TestEntry;

void test_sample(char *fmt, ...)
{
	int i;
	va_list ap;

	char *s, c;
	int d;

	va_start(ap, fmt);
	while (*fmt)
		switch (*fmt++) {
		case 's':					  /* string */
			s = va_arg(ap, char *);
			printf("string %s\n", s);
			break;
		case 'd':					  /* int */
			d = va_arg(ap, int);
			printf("int %d\n", d);
			break;
		case 'c':					  /* char */
			/* need a cast here since va_arg only
			   takes fully promoted types */
			c = (char)va_arg(ap, int);
			printf("char %c\n", c);
			break;
		}
	va_end(ap);

	printf("sample test start argv \n");
	return;
}

void test_another(char *fmt, ...)
{
	int i;

	printf("another test start argv \n");
	return;
}

#define asizeof(a)     (int)(sizeof (a) / sizeof ((a)[0]))

_TestEntry TestEntry[] = {
	{"sample", test_sample} ,
	{"another", test_another}
};

int main(int argc, char **argv)
{
	int i, opt;
	char *testname = NULL;
	long ncount;

	if (argc < 3) {
		printf("usage: %s -t [sample|another]\n", argv[0]);
		exit(0);
	}
	while ((opt = getopt(argc, argv, "f:d:n:t:m:b:")) != -1) {
		switch (opt) {
		case 'n':
			ncount = atol(optarg);
			break;
		case 't':
			testname = strdup(optarg);
			break;
		default:
			break;
		}
	}

	if (testname == NULL) {
		printf("usage: %s -t [sample|another]\n", argv[0]);
		exit(0);
	}

	for (i = 0; i < asizeof(TestEntry); i++) {
		if (strncmp(TestEntry[i].fname, testname, strlen(testname)) == 0)
			TestEntry[i].test_func("%d", argc);
	}

	return (0);
}
